﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoNiuOA.Models;
using PagedList;

namespace TaoNiuOA.Web.Controllers
{
    public class ApplyController : Controller
    {
        private DbEntity db = new DbEntity();

        //分页
        int pageSize = 2;

        [IsAdmin]
        public ViewResult Index(int? page)
        {
            var index = (page ?? 1) - 1;
            var mod = Request.QueryString["mod"];
            ViewData["mod"] = mod;
            var allApplies = db.Applies;
            //计数
            ViewData["allcount"] = allApplies.Count();
            ViewData["refusecount"] = allApplies.Where(n => n.Status == 0).Count();
            ViewData["agreecount"] = allApplies.Where(n => n.Status == 1).Count();
            ViewData["approvecount"] = allApplies.Where(n => n.Status == 2).Count();
            var list = new List<Apply>();
            switch (mod)
            {
                //拒绝的申请
                case "refuse":
                    list = allApplies.Where(n => n.Status == 0).ToList();
                    break;
                //同意的申请
                case "agree":
                    list = allApplies.Where(n => n.Status == 1).ToList();
                    break;
                //审批中的
                case "approve":
                    list = allApplies.Where(n => n.Status == 2).ToList();
                    break;
                //默认,所有申请
                default:
                    list = allApplies.ToList();
                    break;
            }
           
            return View(list.ToPagedList(index, pageSize));
        }

        [IsAdmin]
        public ViewResult Details(int id)
        {
            Apply apply = db.Applies.Find(id);
            return View(apply);
        }

        // GET: 创建申请
        [IsLogon]
        public ActionResult Create()
        {
            ViewBag.ApplyTypeID = new SelectList(db.ApplyTypes, "ApplyTypeID", "TypeName");
            return View();
        }

        // POST: 创建申请
        [IsLogon]
        [HttpPost]
        public ActionResult Create(Apply apply)
        {
            if (ModelState.IsValid)
            {
                var user = (TaoNiuOA.Models.User)Session["users"];
                apply.Status = 2;
                apply.UserID = user.UserID;
                apply.ApplyTime = DateTime.Now;
                db.Applies.Add(apply);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(apply);
        }

        //审批
        [IsAdmin]
        public ActionResult Agree()
        {
            try
            {
                var user = Session["users"] as TaoNiuOA.Models.User;
                var id = Request.QueryString["id"];
                var temps = Request.QueryString["status"];

                if (!ShenLan.Common.IntHandler.IsNotNullOrInt(id) || !ShenLan.Common.IntHandler.IsNotNullOrInt(temps))
                    return Json("错误的id");

                var appid = Convert.ToInt32(id);
                var status = Convert.ToInt32(temps);
                var apply = db.Applies.SingleOrDefault(n => n.ApplyID == appid);
                if (apply == null)
                    return Json("记录不存在");
                if (apply.Status != 2)
                    return Json("该记录已经审批过了,不能再次审批");

                db.Entry(apply).State = EntityState.Modified;
                apply.Status = status;
                apply.Approver = user.UserID;
                db.SaveChanges();
                return Json("审批成功");

            }
            catch (Exception ex)
            {
                return Json("审批失败");
            }
        }

        public ActionResult click()
        {
            string str = "function show(){alert('666');}";
            return JavaScript(str);
        }

        [IsAdmin]
        public ActionResult Edit(int id)
        {
            Apply apply = db.Applies.Find(id);
            return View(apply);
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Edit(Apply apply)
        {
            if (ModelState.IsValid)
            {
                db.Entry(apply).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(apply);
        }

        public ActionResult Delete(int id)
        {
            Apply apply = db.Applies.Find(id);
            return View(apply);
        }

        [IsAdmin]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Apply apply = db.Applies.Find(id);
            db.Applies.Remove(apply);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}